<?php
/**
 * 赠送规则         
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class PresenterController extends AdmincommonController {
    /**
     * 显示商品列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index() {
        $filters = array();
        !empty($_POST['presentStandard']) && $filters['presentStandard'] = $_POST['presentStandard'];
        $presenter = D("Presenter");
        $_GET['p'] = $_POST['p'];
        $count = $presenter->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $presenter->limit($Page->firstRow.','.$Page->listRows)
                          ->relation(true)
                         ->where($filters)
                         ->select();
        $this->assign('list',$list);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }
    
    /**
     * 显示新增商品页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function add() {
        $gift_list = D("Gifts")->select();
        $this->assign("gift_list",$gift_list);
        $this->display('info');
    }

    /**
     * 处理新增
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function insert() {
        $presenter = D("Presenter");
        if ($vo = $presenter->create()) {
            if(!empty($_POST['time'])) {
                $time  = strtotime($_POST['time']);
                $times = strtotime($_POST['times']);
                if($time > $times){
                    $vo['endTime']  = $time;
                    $vo['startTime']  = $times;
                } else {
                    $vo['startTime']  = $time;
                    $vo['endTime']  = $times;
                }
            }
            $rs = $presenter->add($vo);
            if ($rs !== false) {
                $re['statusCode'] = "200";
                $re['message'] = "新增商品成功!";
                $re['navTabId'] = "Presenterindex";
                $re['callbackType'] = "closeCurrent";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "新增管理员错误，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = $integral->getError();
        }
        echo json_encode($re);
    }

    /**
     * 显示编辑页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function edit() {
        $id = remove_xss($_GET['id']);
        if (!empty($id)) {
            $presenter     = D("Presenter");
            $presenter_info = $presenter->find($id);
            if ($presenter_info) {
                $presenter_info["startTime"] = date("Y-m-d",$presenter_info["startTime"]);
                $presenter_info["endTime"] = date("Y-m-d",$presenter_info["endTime"]);
                $gift_list = D("Gifts")->select();
                $this->assign("gift_list",$gift_list);
                $this->assign('info', $presenter_info);
                $this->display('info');
            } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Presenterindex";
                $re['message'] = "该商品信息不存在！";
                $re['callbackType'] = "closeCurrent";
                echo json_encode($re);
            }
        } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Integralindex";
                $re['message'] = "该商品信息不存在！";
                echo json_encode($re);
        }
    }

    /**
     * 处理更新数据
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function update() {
        $presenter = D("Presenter");
        $re = array();
        if ($vo = $presenter->create()) {
            if(!empty($_POST['time'])) {
                $time  = strtotime($_POST['time']);
                $times = strtotime($_POST['times']);
                if($time > $times){
                    $vo['endTime']  = $time;
                    $vo['startTime']  = $times;
                } else {
                    $vo['startTime']  = $time;
                    $vo['endTime']  = $times;
                }
            }
            $list = $presenter->save($vo);
            if ($list !== false) {
                $re['statusCode'] = "200";
                $re['message'] = "商品信息修改成功!";
                $re['navTabId'] = "Presenterindex";
                $re['callbackType'] = "closeCurrent";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "没有更新任何数据!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = $presenter->getError();
        }
        echo json_encode($re);
    }

    /**
     * 删除商品
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        $re = array();
        if (!empty($_GET['id'])) {
            $presenter = D("Presenter");
            $result = $presenter->delete(remove_xss($_GET['id']));
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $presenter = D("Presenter");
        $re = array();
        $map['id']  = array('in', explode(',', $_POST["ids"]));
        $result  = $presenter->where($map)->delete();
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }

    /**
     * 改变状态
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function update_status() {
        $re = array();
        if (!empty($_GET['id'])) {
            $presenter = D("Presenter");
            $result = $presenter->find(remove_xss($_GET['id']));
            if ($result["status"] == 1) {
                $data["status"] = 0;
            }else{
                $data["status"] = 1;
            }
            $data["id"]= $result["id"];
            $rs = $presenter->save($data);
            if (false !== $rs) {
                $re['statusCode'] = "200";
                $re['message'] = "操作成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "操作失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

}
?>